﻿Imports QLPhuTungOto.Data
Imports QLPhuTungOto.Bus
Public Class frmThemKhachHang

    Dim KH_BUS As New KhachHangBUS

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Close()
    End Sub

    Private Sub txtSDT_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSDT.KeyPress

        If Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Then
        Else
            e.Handled = True
        End If

    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Try
            Dim err As String = ""
            err = KiemTraNhap()

            If err <> "" Then
                MessageBox.Show(err, "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Else
                Dim trung As String = ""
                trung = KiemTraTrung()
                If trung <> "" Then
                    If MessageBox.Show(trung, "Câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                        them()
                        TrangThai.ThayDoiKhachHang()
                        Close()
                    End If
                Else
                    them()
                    TrangThai.ThayDoiKhachHang()
                    Close()
                End If
            End If

        Catch ex As Exception
            MessageBox.Show("Không thể thêm khách hàng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Function KiemTraNhap() As String
        Dim err As String = ""
        err += HamHoTro.KiemTraRong(txtTenKhachHang.Text, "Tên khách hàng")
        Return err
    End Function

    Private Function KiemTraTrung() As String
        Dim err As String = ""
        Dim kh As New KhachHang
        KH_BUS = New KhachHangBUS
        kh = KH_BUS.LayKhachHangTheoTen(txtTenKhachHang.Text)
        If kh.TenKhachHang = txtTenKhachHang.Text Then
            err += "Tên khách hàng này đã tồn tại, bạn có muốn tiếp tục không?"
        End If

        Return err
    End Function

    Private Sub them()
        Dim kh As New KhachHang With {.TenKhachHang = txtTenKhachHang.Text,
                                      .Sdt = txtSDT.Text}
        KH_BUS.Them(kh)
    End Sub
End Class